package com.zkthink.ceres.app.address.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zkthink.base.R;
import com.zkthink.ceres.member.dto.MemberAddressPageDTO;
import com.zkthink.ceres.member.dto.MemberAddressSaveDTO;
import com.zkthink.ceres.member.dto.MemberAddressUpdateDTO;
import com.zkthink.ceres.member.entity.MemberAddress;
import com.zkthink.ceres.member.service.MemberAddressService;
import com.zkthink.context.BaseContextHandler;
import com.zkthink.dozer.DozerUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by kellen on 2020/5/4.会员地址管理
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/address")
@Api(value = "address", tags = "地址服务")
@AllArgsConstructor
public class AppMemberAddressController {

  private MemberAddressService memberAddressService;

  private DozerUtils dozerUtils;


  @ApiOperation(value = "保存收货地址", notes = "保存收货地址")
  @PostMapping("/add")
  public R add(@RequestBody MemberAddressSaveDTO addressSaveDTO) {
    addressSaveDTO.setMid(BaseContextHandler.getUserId());
    MemberAddress memberAddress = BeanUtil.toBean(addressSaveDTO, MemberAddress.class);
    memberAddressService.add(memberAddress);
    return R.success();
  }


  @ApiOperation(value = "更新收货地址", notes = "更新收货地址")
  @PostMapping("/update")
  public R update(@RequestBody MemberAddressUpdateDTO updateDTO) {
    //查询收货地址
    MemberAddress address = memberAddressService.getById(updateDTO.getId());
    if (null == address) {
      return R.fail("收货地址不存在");
    }
    Long mid = BaseContextHandler.getUserId();
    if (!mid.equals(address.getMid())) {
      return R.fail("非法操作");
    }
    address = new MemberAddress();
    BeanUtil.copyProperties(updateDTO, address);
    address.setMid(mid);
    memberAddressService.add(address);
    return R.success();
  }

  @ApiOperation(value = "删除收货地址", notes = "删除收货地址")
  @PostMapping("/delete")
  public R delete(@RequestBody MemberAddressUpdateDTO updateDTO) {
    //删除收货地址
    MemberAddress address = memberAddressService.getById(updateDTO.getId());
    if (null == address) {
      return R.fail("收货地址不存在");
    }
    Long mid = BaseContextHandler.getUserId();
    if (!mid.equals(address.getMid())) {
      return R.fail("非法操作");
    }
    address.setIsDeleted(true);
    memberAddressService.updateById(address);
    return R.success();
  }


  /**
   * 查询用户所有的收货地址
   */
  @GetMapping("/queryMemberAddress")
  @ApiOperation(value = "查询用户所有收货地址", notes = "查询用户所有收货地址")
  public R<List<MemberAddressPageDTO>> queryMemberAddress() {
    //获取用户mid
    Long mid = BaseContextHandler.getUserId();
    return R.success(
        dozerUtils.mapList(memberAddressService.queryByMid(mid), MemberAddressPageDTO.class));
  }

  /**
   * 地址详情
   */
  @GetMapping("/detail/{id}")
  @ApiOperation(value = "地址详情", notes = "地址详情", response = MemberAddressPageDTO.class)
  public R<MemberAddressPageDTO> addressDetail(@PathVariable Integer id) {
    return R.success(dozerUtils.map(memberAddressService.getById(id), MemberAddressPageDTO.class));
  }


}
